package ar.unrn.hibernate.main;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

import ar.unrn.hibernate.model.Student;

public class Main {

	public static void main(String[] args) {

		Configuration configuration = new Configuration().configure();
		
		StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
				.applySettings(configuration.getProperties());
		
		SessionFactory factory = configuration.buildSessionFactory(builder
				.build());

		Session session = factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			// do some work

			Student angus = new Student("Angus", "Young");
			session.save(angus);
			
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

	}

}
